package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.dto.ApBehaviorEntry;
import com.heima.article.dto.CollectionBehaviorDto;
import com.heima.article.dto.EntryDto;
import com.heima.article.dto.UpdateArticleMessage;
import com.heima.article.entity.ApCollection;
import com.heima.article.feign.BehaviorFeign;
import com.heima.article.mapper.ApCollectionMapper;
import com.heima.article.service.IApCollectionService;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP收藏信息表 服务实现类
 * </p>
 *
 * @author wsj
 * @since 2022-03-11
 */
@Service
public class ApCollectionServiceImpl extends ServiceImpl<ApCollectionMapper, ApCollection> implements IApCollectionService {
    @Autowired
    private BehaviorFeign behaviorFeign;
    @Value("${topic.hotArticleScoreTopic}")
    private String hotArticleScoreTopic;
    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    @Override
    public ResponseResult collection(CollectionBehaviorDto dto) {
        //收藏文章
        //获取线程中的user
        User user = AppThreadLocalUtil.get();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //保存实体
        EntryDto entryDto = new EntryDto();
        entryDto.setEquipmentId(dto.getEquipmentId());
        entryDto.setUserId(user.getId());
        ResponseResult<ApBehaviorEntry> result = behaviorFeign.saveEntry(entryDto);
        ApBehaviorEntry entry = null;
        if (result.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {
            entry = result.getData();
        }
        //判断收藏是否存在
        LambdaQueryWrapper<ApCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApCollection::getArticleId, dto.getArticleId());
        wrapper.eq(ApCollection::getEntryId, entry.getId());
        ApCollection apCollection = getOne(wrapper);
        if (apCollection != null) {
            //存在，则判断是否是取消收藏
            if (dto.getOperation()==1) {
                //取消收藏，删除记录
                removeById(apCollection);
                //发送消息到kafka
                UpdateArticleMessage message = new UpdateArticleMessage();
                message.setAdd(-1);
                message.setArticleId(apCollection.getArticleId());
                message.setType(3);
                String jsonString = JSON.toJSONString(message);
                kafkaTemplate.send(hotArticleScoreTopic,jsonString);
            }
            return ResponseResult.okResult();
        } else {
            //不存在，新增
            apCollection = new ApCollection();
            apCollection.setArticleId(Long.parseLong(dto.getArticleId()));
            apCollection.setCollectionTime(new Date());
            apCollection.setEntryId(entry.getId());
        }
        saveOrUpdate(apCollection);
        //发送消息到kafka
        UpdateArticleMessage message = new UpdateArticleMessage();
        message.setAdd(1);
        message.setArticleId(apCollection.getArticleId());
        message.setType(3);
        String jsonString = JSON.toJSONString(message);
        kafkaTemplate.send(hotArticleScoreTopic,jsonString);
        //返回响应
        return ResponseResult.okResult();
    }
}
